import { getSelector } from '@/api/crm/school'
import request from '@/utils/request'
export default {
  components: {},
  props: [],
  data() {
    return {
      loading: false,
      setting: {},
      dataForm: {
        id: undefined,
        customerId: undefined,
        brand: undefined,
        manageId: undefined,
        primarySchool: undefined,
        type: '2',
        amount: undefined,
        paymentDate: undefined,
        finalPaymentDate: undefined,
        screenshot: [],
        remarks: undefined
      },
      rules: {
        brand: [
          {
            required: true,
            message: '请选择校区品牌',
            trigger: 'change'
          }
        ],
        primarySchool: [
          {
            required: true,
            message: '请输入意向小学/中学',
            trigger: 'change'
          }
        ],
        amount: [
          {
            required: true,
            message: '请输入缴费金额',
            trigger: 'change'
          }
        ],
        paymentDate: [
          {
            required: true,
            message: '请选择缴费时间',
            trigger: 'change'
          }
        ],
        finalPaymentDate: [
          {
            required: true,
            message: '请选择尾款时间',
            trigger: 'change'
          }
        ],
        manageId: [
          {
            required: true,
            message: '请选择招商经理',
            trigger: 'change'
          }
        ],
        customerId: [
          {
            required: true,
            message: '请选择客户',
            trigger: 'change'
          }
        ]
      },
      brandOptions: [],
      typeOptions: [{ fullName: '全款', id: '1' }, { fullName: '定金', id: '2' }, { fullName: '保证金', id: '3' }, { fullName: '余款', id: '4' }],
      userList: []
    }
  },
  computed: {},
  watch: {},
  created() {
    this.getUserList()
    this.$store.dispatch('base/getDictionaryData', { sort: 'Brand' }).then(res => {
      this.brandOptions = res
    })
  },
  mounted() {},
  methods: {
    handleSelectCustomer(item) {
      this.dataForm.customerId = item.id
      this.dataForm.customerName = item.name
    },
    getUserList() {
      this.$store.dispatch('base/getUserList').then(res => {
        this.userList = res.filter(o => o.enabledMark == 1 && o.positionId.indexOf('445842201948194053') > -1)
      })
    },
    goBack() {
      this.$emit('refresh')
    },
    querySearchSchoolAsync(queryString, cb) {
      clearTimeout(this.timeout)
      this.timeout = setTimeout(() => {
        if (queryString)
          getSelector(queryString).then(res => {
            let list = res.data.map(item => {
              item['value'] = item.name + '(' + item.mobile + ')'
              return item
            })
            cb(list)
          })
      }, 3000 * Math.random())
    },
    init(data) {
      this.setting = data
      this.dataForm.id = data.id || ''
      if (this.dataForm.id) {
        this.$nextTick(() => {
          this.$refs['elForm'].resetFields()
          request({
            url: '/api/crm/customer/paymentrecords/' + this.dataForm.id,
            method: 'get'
          }).then(res => {
            this.dataForm = res.data
            this.loading = false
          })
        })
      } else {
        this.dataForm.customerId = data.customerId || ''
      }
    },
    dataFormSubmit(eventType) {
      this.$refs['elForm'].validate(valid => {
        if (valid) {
          if (eventType === 'audit' || eventType === 'reject') {
            this.$emit('eventReciver', this.dataForm, eventType)
            return
          }
          this.dataForm.flowState = eventType === 'submit' ? 0 : 1
          this.eventType = eventType
          if (this.eventType === 'submit') {
            this.$confirm('您确定要提交当前流程吗, 是否继续?', '提示', {
              type: 'warning'
            })
              .then(() => {
                this.request()
              })
              .catch(() => {})
          } else {
            this.request()
          }
        }
      })
    },
    request() {
      this.dataForm.id
        ? request({
            url: `/api/crm/customer/paymentrecords`,
            method: 'put',
            data: this.dataForm
          }).then(res => {
            this.$message({
              message: res.msg,
              type: 'success',
              duration: 1000,
              onClose: () => {
                this.$emit('close', true)
              }
            })
          })
        : request({
            url: `/api/crm/customer/paymentrecords`,
            method: 'post',
            data: this.dataForm
          }).then(res => {
            this.$message({
              message: res.msg,
              type: 'success',
              duration: 1000,
              onClose: () => {
                this.$emit('close', true)
              }
            })
          })
    }
  }
}
